package cn.stronglink.assetmanage.business.inventory.model;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.task.model.ITaskSyncListener;
import cn.stronglink.assetmanage.business.task.model.OrderType;
import cn.stronglink.assetmanage.business.task.model.TaskSyncModel;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.InventoryDao;
import cn.stronglink.assetmanage.dao.InventoryDetailDao;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.entity.InventoryDetail;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.netrequest.business.inventory.InventoryService;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by chencongcong on 2018/6/15.
 */

public class InventorySyncModel {
    private Gson gson = new Gson();
    private InventoryService inventoryService;
    private OnInventorySyncDataListener inventorySyncDataListener;
    List<Inventory> inventoryList = null;

    public InventorySyncModel() {
        inventoryService = new InventoryService();
    }

    public OnInventorySyncDataListener getInventorySyncDataListener() {
        return inventorySyncDataListener;
    }

    public void setInventorySyncDataListener(OnInventorySyncDataListener inventorySyncDataListener) {
        this.inventorySyncDataListener = inventorySyncDataListener;
    }

    //获得所有已盘点数据表单  已盘点为非1
    public List<Inventory> getDataByOrderNoDistince() {
        List<Inventory> inventoryList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDao().queryBuilder().distinct()
                .orderDesc(InventoryDao.Properties.Id_).
                        where(InventoryDao.Properties.Status.notEq(Constants.STATUS_ONE)).list();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventoryList != null) {
            return inventoryList;
        }
        return null;
    }

    /**
     * 根据表单号获得二级列表,非1代表已完成
     *
     * @param id_
     * @return
     */
    public Map<String, List<InventoryDetail>> getDataByOrderNo(String id_) {
        //获得二级列表
        Map<String, List<InventoryDetail>> inventoryList = new HashMap<String, List<InventoryDetail>>();
        List<InventoryDetail> inventoryDetailList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().queryBuilder().
                where(InventoryDetailDao.Properties.Id_.eq(id_)).
                where(InventoryDetailDao.Properties.Status.notEq(Constants.STATUS_ONE))
                .where(InventoryDetailDao.Properties.IsAdd.eq(false)).list();
        inventoryList.put(MyApplication.APP_CONTEXT.getResources().getString(R.string.inventory_normal),
                inventoryDetailList);
        List<InventoryDetail> inventoryDetailList1 = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().queryBuilder().
                where(InventoryDetailDao.Properties.Id_.eq(id_)).
                where(InventoryDetailDao.Properties.Status.notEq(Constants.STATUS_ONE))
                .where(InventoryDetailDao.Properties.IsAdd.eq(true)).list();
        inventoryList.put(MyApplication.APP_CONTEXT.getResources().getString(R.string.inventory_more),
                inventoryDetailList1);
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventoryList != null) {
            return inventoryList;
        }
        return null;
    }

    public List<InventoryDetail> getDetailListByOrderNo(String orderNo) {
        List<InventoryDetail> detailList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().queryBuilder().
                where(InventoryDetailDao.Properties.Id_.eq(orderNo)).
                where(InventoryDetailDao.Properties.Status.notEq(Constants.STATUS_ONE)).list();
        if (detailList != null) {
            return detailList;
        }
        return null;
    }

    public List<InventoryDetail> getDetailListByOrderNo(String orderNo,int state) {
        List<InventoryDetail> detailList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().queryBuilder().
                where(InventoryDetailDao.Properties.Id_.eq(orderNo)).
                where(InventoryDetailDao.Properties.Status.eq(state)).list();
        if (detailList != null) {
            return detailList;
        }
        return null;
    }

    public void syncInventoryData() {


    }

    /**
     * 获得需要同步的盘点数据
     *
     * @return
     */
    public List<Inventory> getInventorySyncData() {
        inventoryList = getDataByOrderNoDistince();
        if (inventoryList != null && inventoryList.size() > 0) {
            for (int i = 0; i < inventoryList.size(); i++) {
                Inventory inventory = inventoryList.get(i);
                inventory.setId(Long.valueOf(inventory.getId_()));
                // 添加正常盘点到的资产
                List<InventoryDetail> inventoryDetailList = getDetailListByOrderNo(inventory.getId_(),3);
                if (inventoryDetailList != null && inventoryDetailList.size() > 0) {
                    for(int j=0;j<inventoryDetailList.size();j++){
                        InventoryDetail inventoryDetail = inventoryDetailList.get(j);
                        inventoryDetail.setId(Long.valueOf(inventoryDetail.getId_()));
                    }

                }
                inventory.setDetailList(inventoryDetailList);

                // 添加盘盈到的资产
                List<InventoryDetail> inventoryMoreDetailList = getDetailListByOrderNo(inventory.getId_(),5);
//                if (inventoryMoreDetailList != null && inventoryMoreDetailList.size() > 0) {
//                    for(int j=0;j<inventoryMoreDetailList.size();j++){
//                        InventoryDetail inventoryMoreDetail = inventoryMoreDetailList.get(j);
//                        inventoryMoreDetail.setId(Long.valueOf(inventoryMoreDetail.getId_()));
//                    }
//
//                }
                inventory.setMoreOutList(inventoryMoreDetailList);
            }

            return inventoryList;
        }
        return null;
    }

    public void deleteSyncData() {
        if (inventoryList != null && inventoryList.size() > 0) {
            for (int i = 0; i < inventoryList.size(); i++) {
                Inventory inventory = inventoryList.get(i);
                List<InventoryDetail> inventoryDetailList = getDetailListByOrderNo(inventory.getId_());
                if (inventoryDetailList != null && inventoryDetailList.size() > 0) {
                    for (int j = 0; j < inventoryDetailList.size(); j++) {
                        InventoryDetail inventoryDetail = inventoryDetailList.get(i);
                        DaoManager.
                                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().delete(inventoryDetail);
                    }
                }
                DaoManager.
                        getDaoSession(MyApplication.APP_CONTEXT).getInventoryDao().delete(inventory);
            }
        }


    }

    /**
     * 根据单号id获得
     *
     * @param orderNo
     * @return
     */
    public Inventory getInventoryByOrderNo(String orderNo) {
        Inventory inventory = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDao().queryBuilder().distinct()
                .orderDesc(InventoryDao.Properties.Id_).
                        where(InventoryDao.Properties.Id_.eq(orderNo)).
                        where(InventoryDao.Properties.Status.notEq(Constants.STATUS_ONE)).list().get(0);
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventory != null) {
            return inventory;
        }
        return null;
    }

    public interface OnInventorySyncDataListener {
        void onFailure(Call call, IOException ex);

        void onResponse(Call call, Response response);
    }
}
